#include <bits/stdc++.h>

using namespace std;
using ll=long long;


template<typename T=int>
inline T read() {
    T x;
    cin >> x;
    return x;
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    for (int T = read(); T--;) {
        int n = read();
        vector<ll> a(n), b(n);
        for (auto &e:a)cin >> e;
        for (auto &e:b)cin >> e;
        sort(a.begin(), a.end(), less<ll>());
        sort(b.begin(), b.end(), greater<ll>());
        cout << inner_product(a.begin(), a.end(), b.begin(), 0LL) << endl;
    }

    return 0;
}